

//                 A
//         B            C
//        D    E       F  G
//           H   J



function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}

var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
var f = new Node("f");
var g = new Node("g");
var h = new Node("h");
var j = new Node("j");

a.left = b
a.right = c
b.left = d
// b.right = e      //删除节点e
c.left= f
c.right = g
d.right = h
e.right = j

function getDeep(root) {
	if( root === null) return 0
	let leftDeep = getDeep(root.left)
	let rightDeep = getDeep(root.right)
	return Math.max(leftDeep, rightDeep) + 1
}

function isBalance(root) {
	if(root === null) return true
	let leftDeep = getDeep(root.left)
	let rightDeep = getDeep(root.right)
	if(Math.abs(leftDeep - rightDeep) > 1 ) {  //两树的绝对值 > 1  不平衡
		return false
	} else {
		return isBalance(root.left) && isBalance(root.right)
	}
}

console.log(isBalance(b))
